package com.situ.stl.controller;

import com.situ.stl.constants.Constants;
import com.situ.stl.entity.Admin;
import com.situ.stl.entity.Coach;
import com.situ.stl.service.AdminService;
import com.situ.stl.service.CoachService;
import com.situ.stl.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/coach")
@Slf4j
public class CoachController {
    @Autowired
    private CoachService coachService;
    @GetMapping
    public Result getAll(Integer page, Integer limit, Coach coach) {
        if (ObjectUtils.isEmpty(page) && ObjectUtils.isEmpty(limit)) {
            List<Coach> coachList = coachService.select(coach);
            return Result.success(Constants.STATUS_200, coachList);
        }
        return Result.success(Constants.STATUS_200, coachService.searchByPage(page, limit, coach));
    }
    @DeleteMapping("/{id}")
    public Result deleteCoach(@PathVariable Integer id) throws Exception {
        int row = coachService.deleteCoach(id);
        if(row > 0) {
            return Result.success(Constants.STATUS_200);
        } else {
            return Result.success(Constants.STATUS_10001,"删除教练失败");
        }
    }
    @PutMapping
    public Result modifyCoach(@RequestBody Coach coach){
        try {
            int row = coachService.modifyCoach(coach);
            if (row == 0) {
                return Result.error(Constants.EDIT_Coach_ERROR,"修改教练失败");
            }else if (row == Constants.PHONE_HAS_ERROR) {
                return Result.success(Constants.PHONE_HAS_ERROR, "手机号被占用");
            }else if (row == Constants.WECHAT_HAS_ERROR) {
                return Result.success(Constants.WECHAT_HAS_ERROR, "微信号被占用");
            }

        }catch (Exception e){
            log.info("修改失败");
            return Result.success(Constants.STATUS_10001,"修改教练失败");
        }
        return Result.success(Constants.STATUS_200);
    }
    @PostMapping
    public Result add(@RequestBody Coach coach){

        try {
            int row = coachService.addCoach(coach);
            if (row == 0) {
                return Result.error(Constants.EDIT_Coach_ERROR,"添加教练失败");
            }else if (row == Constants.PHONE_HAS_ERROR) {
                return Result.success(Constants.PHONE_HAS_ERROR, "手机号被占用");
            }else if (row == Constants.WECHAT_HAS_ERROR) {
                return Result.success(Constants.WECHAT_HAS_ERROR, "微信号被占用");
            }
        }catch (Exception e){
            log.info("添加失败");
            return Result.success(Constants.STATUS_10001,"添加教练失败");
        }
        return Result.success(Constants.STATUS_200);

    }
}
